package com.hd.trans.share;

import android.content.Context;
import android.os.Environment;

import com.hd.trans.ui.base.AppContextProvider;

import org.jetbrains.annotations.NotNull;

import java.io.File;


/**
 * 文件路径管理
 */
public class FileConfigManager {
    public static final String HD_CACHE_FOLDER_NAME = "hudunfilelib";
    private static final String MODEL_VIVO_X9P = "vivo X9Plus";
    private static final String MODEL_VIVO_Y3 = "V1901A";

    public static void initFileDir(Context context) {
        /*PermissionX.init((FragmentActivity) context)
                .permissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, @NonNull List<String> grantedList, @NonNull List<String> deniedList) {
                        if (allGranted) {
                            File videoFile = new File(getVideoCacheFilePath());
                            if (!videoFile.exists()) {
                                videoFile.mkdirs();
                            }
                            File shareFile = new File(getShareCacheFilePath());
                            if (!shareFile.exists()) {
                                shareFile.mkdirs();
                            }
                            File temFile = new File(getMergeImgFilePath(context));
                            if (!temFile.exists()) {
                                temFile.mkdirs();
                            }
                            File srtFile = new File(getSrtCacheFilePath());
                            if (!srtFile.exists()) {
                                srtFile.mkdirs();
                            }
                            File transFile = new File(getTransCacheFilePath());
                            if (!transFile.exists()) {
                                transFile.mkdirs();
                            }
                            File wgFile = new File(getAudioTrackCacheFilePath());
                            if (!wgFile.exists()) {
                                wgFile.mkdirs();
                            }
                            File pcmFile = new File(getTempPcmFilePath());
                            if (!pcmFile.exists()) {
                                pcmFile.mkdirs();
                            }
                            File cacheFile = new File(getCacheDirSharePath());
                            if (!cacheFile.exists()) {
                                cacheFile.mkdirs();
                            }
                            File wgTagFile = new File(getAudioTrackTagCacheFilePath());
                            if (!wgTagFile.exists()) {
                                wgTagFile.mkdirs();
                            }
                        }
                    }
                });*/
    }

    private static String getHdBaseCacheFolderPath() {
//        return Environment.getExternalStorageDirectory() + File.separator + HD_CACHE_FOLDER_NAME + File.separator;
        return Environment.getExternalStorageDirectory() + File.separator + HD_CACHE_FOLDER_NAME + File.separator;
    }

    /**
     * 合成的视频缓存路径，系统相册
     */
    public static String getVideoCacheFilePath() {
        //默认路径，存储至DCIM/hudunfilelib
        String mPhotoPath;
        switch (android.os.Build.MODEL) {
            case MODEL_VIVO_X9P:
                mPhotoPath = Environment.getExternalStorageDirectory() + File.separator + "相机";
                break;
            case MODEL_VIVO_Y3:
                mPhotoPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "Camera";
                break;
            default:
                mPhotoPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + HD_CACHE_FOLDER_NAME;
                break;
        }
        return mPhotoPath;
    }

    /**
     * 分享文件缓存
     */
    public static String getShareCacheFilePath() {
        return getHdBaseCacheFolderPath() + "shareCache";
    }

    /**
     * wav  mp3 等格式音频
     * 音频文件
     */
    @NotNull
    public static String getAudioCacheFilePath() {
        return getHdBaseCacheFolderPath() + "music";
    }

    /**
     * 合成视频的图片临时路径
     */
    public static String getMergeImgFilePath(Context context) {
        return context.getExternalCacheDir() + File.separator + "image";
    }

    /**
     * 识别文字 srt路径
     */
    @NotNull
    public static String getSrtCacheFilePath() {
        return getHdBaseCacheFolderPath() + "srt";
    }

    /**
     * 文件翻译 translate路径
     */
    @NotNull
    public static String getTransCacheFilePath() {
        String path = getHdBaseCacheFolderPath() + "translate";
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        return path;
    }

    /**
     * 音轨信息 .WG2
     */
    @NotNull
    public static String getAudioTrackCacheFilePath() {
        return getHdBaseCacheFolderPath() + "audiotrack";
    }

    /**
     * 音频标签 存储位置
     *
     * @return
     */
    public static String getAudioTrackTagCacheFilePath() {
        return getHdBaseCacheFolderPath() + "audiotracktag";
    }

    /**
     * pcm 缓存路径
     *
     * @return
     */
    public static String getTempPcmFilePath() {
        return getHdBaseCacheFolderPath() + "tempPCM";
    }

    /**
     * 分享文件路径/doc/pdf 分享文件路径
     *
     * @return
     */
    public static String getCacheDirSharePath() {
        return AppContextProvider.getContext().getExternalCacheDir() + File.separator + "share";
//        return ShareTextFileManager.app.getExternalCacheDir() + File.separator + "share";
//        return Environment.getExternalStorageDirectory() + File.separator + "share";
    }

    /**
     * 根据时间戳生成文件名字
     *
     * @return 01月08日 下午 16时24分22秒
     */
    @NotNull
    public static String createNewFileName() {
        return AudioTimeFormatUtil.formatCreateFileName(System.currentTimeMillis());
    }
}
